<?php
require '../data/data.php';


session_start();

if (!isset($_SESSION['user'])) {
	header('Location:../../public/register.php?op=2');
	return;
}

$user = $_SESSION['user'];
$userName = $user['user_name'];
$content = isset($_POST['content']) ? htmlspecialchars($_POST['content']) : '';

$hasError = false;
$errorBag = ['content' => 'The content should not be empty'];
if ($content == '') {
	$hasError = true;
	$_SESSION['has_error'] = $hasError;
	$_SESSION['error_bag'] = $errorBag;
	header('Location:../../public/index.php');
	return;
}

$sql = 'insert into messages(user_name, content, created_at) values(?, ?, now())';
$args = [$userName, $content];
execute($sql, $args);
header('Location:../../public/index.php');